package com.cp.yn.fragment

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.common.base.base.BaseFragment
import com.common.base.tools.arouter.startRouter
import com.cp.yn.R
import com.cp.yn.databinding.FragmentMineBinding
import com.cp.yn.manager.RouterManager
import com.lxj.xpopup.XPopup
import com.common.base.tools.extend.click
import com.common.base.tools.extend.stringRes
import com.king.app.dialog.AppDialog
import com.king.app.dialog.AppDialogConfig
import com.king.app.updater.AppUpdater
import kotlinx.coroutines.delay


/**
 * 三疯
 * *************************
 *
 * *************************
 */
class MineFragment : BaseFragment<FragmentMineBinding>() {

    override fun viewBinding(): FragmentMineBinding {
        return FragmentMineBinding.inflate(layoutInflater)
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        super.initView(view, savedInstanceState)
        viewBinding.apply {
            mineLogout.click {
                XPopup.Builder(requireContext())
                    .isDestroyOnDismiss(true)
                    .isViewMode(true)
                    .dismissOnBackPressed(false)
                    .asConfirm(
                        "",
                        stringRes(R.string.mine_logout),
                        stringRes(R.string.login_cancel),
                        stringRes(R.string.login_confirm),
                        {
                            startRouter(RouterManager.Path.APP_Login)
                            requireActivity().finish()
                        },
                        { },
                        false
                    ).show()
            }
            mineLanguage.click {
                startRouter(RouterManager.Path.APP_Language)
            }

            mineVersion.click {
                val url = "https://cloupick-source.oss-cn-hangzhou.aliyuncs.com/app-package/yunna/\$NbzIkwXR2GLaV_3DNs3mM_app-n1_normal-release.apk"

                //简单弹框升级
                val config = AppDialogConfig(requireContext())
                config.setTitle("简单弹框升级")
                    .setConfirm("升级") //旧版本使用setOk
                    .setContent("1、新增某某功能、\n2、修改某某问题、\n3、优化某某BUG、").onClickConfirm =
                    View.OnClickListener{
                    AppUpdater.Builder(requireContext())
                        .setUrl(url)
                        .build()
                        .start()
                    AppDialog.INSTANCE.dismissDialog()
                }
                AppDialog.INSTANCE.showDialog(context, config)
            }

            CircleImageView.click {
                val maxAngle = 230f
                lifecycleScope.launchWhenCreated {
                    var angle = 0f
                    while (angle<=maxAngle){
                        CircleImageView.setProgress(angle)
                        delay(10)
                        angle+=1
                    }
                }


            }


        }
    }
}